package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizOrder;
import com.runvp.service.biz.service.IBizOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 * 
 * @author runvp
 * @date 2022-12-27
 */
@Controller
@RequestMapping("/biz/order")
public class BizOrderController extends BaseController {
    private String prefix = "biz/order";

    @Autowired
    private IBizOrderService bizOrderService;


    @RequiresPermissions("biz:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("biz:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizOrder bizOrder) {
        startPage();
        List<BizOrder> list = bizOrderService.selectBizOrderList(bizOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("biz:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizOrder bizOrder) {
        List<BizOrder> list = bizOrderService.selectBizOrderList(bizOrder);
        ExcelUtil<BizOrder> util = new ExcelUtil<BizOrder>(BizOrder.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @ParamValid
    @RequiresPermissions("biz:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizOrder bizOrder) {
        return toAjax(bizOrderService.insertBizOrder(bizOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("biz:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizOrder bizOrder = bizOrderService.selectBizOrderById(id);
        mmap.put("bizOrder", bizOrder);
        return prefix + "/edit";
    }
    /**
     * 订单详情
     */
    @RequiresPermissions("biz:order:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        BizOrder bizOrder = bizOrderService.selectBizOrderById(id);
        mmap.put("bizOrder", bizOrder);
        return prefix + "/detail";
    }
    /**
     * 修改保存订单
     */
    @ParamValid
    @RequiresPermissions("biz:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizOrder bizOrder) {
        return toAjax(bizOrderService.updateBizOrder(bizOrder));
    }
    /**
     * 关闭订单
     */
    @RequiresPermissions("biz:order:close")
    @Log(title = "关闭订单", businessType = BusinessType.OTHER)
    @GetMapping("/close")
    @ResponseBody
    public AjaxResult close(Long id) {
        return toAjax(bizOrderService.close(id));
    }
    /**
     * 删除订单
     */
    @RequiresPermissions("biz:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizOrderService.deleteBizOrderByIds(ids));
    }
}
